package control.manager
{
	import control.util.CustomLoader;
	import control.util.CustomURLLoader;
	
	import data.BitmapItem;
	import data.UIVO;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.Dictionary;

	/**
	 * 加载管理器 
	 * @author leo
	 * 
	 */	
	public class LoaderManager
	{
		private static var _instance :LoaderManager = null;
		/**
		 * 加载器列表 
		 */		
		public var loaderDict :Dictionary;
		/**
		 * 加载过的文件路径 
		 */		
		public var fileList :Dictionary;
		/**
		 * 文本字典 
		 */		
		public var txtDict :Dictionary;
		
		public var rhand :Function;
		public var fhand :Function;
		
		private var urlLoaderAll :int = 0;
		private var curIndex :int = 0;
		public function LoaderManager(prvClass :PrivateClass)
		{
			this.loaderDict = new Dictionary();
			fileList = new Dictionary();
			this.txtDict = new Dictionary();
		}
		
		/**
		 * load单一显示对象，例如swf 
		 * @param url
		 * @param rhand
		 * @param fhand
		 * 
		 */		
		public function loadItem(url :String,rhand :Function,fhand :Function):void
		{
			if(!url)
			{
				trace("loadItem have nothing");
				return;
			}
			this.rhand = rhand;
			this.fhand = fhand;
			var loader :CustomLoader = new CustomLoader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			
			loader.url = url;
			var start :int = url.lastIndexOf("/") + 1;
			var end :int = url.lastIndexOf(".");
			var fileName :String = url.substring(start,end);
			loader.fileName = fileName;
			var loaderContext :LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
			loader.load(new URLRequest(url),loaderContext);
			this.loaderDict[fileName] = loader;
		}
		
		public function loadTxts(arr :Array,rhand :Function,fhand :Function):void
		{
			if(!arr || arr.length == 0)
			{
				trace("loadTxts have nothing");
				return;
			}
			
			this.rhand = rhand;
			this.fhand = fhand;
			
			this.urlLoaderAll = arr.length;
			this.curIndex = 0;
			for each(var str :String in arr)
			{
				var loader :CustomURLLoader = new CustomURLLoader();
				loader.dataFormat = URLLoaderDataFormat.TEXT;
				loader.addEventListener(Event.COMPLETE,urlLoaderComplete);
				loader.addEventListener(IOErrorEvent.IO_ERROR,urlLoderError);
				loader.url = str;
				var start :int = str.lastIndexOf("/") + 1;
				var end :int = str.lastIndexOf(".");
				var fileName :String = str.substring(start,end);
				loader.fileName = fileName;
				loader.load(new URLRequest(str));
				this.loaderDict[fileName] = loader;
			}
			
		}
		
		private function urlLoaderComplete(event :Event):void
		{
			this.curIndex++;
			var urlLoader :CustomURLLoader = event.target as CustomURLLoader;
			urlLoader.removeEventListener(Event.COMPLETE,urlLoaderComplete);
			urlLoader.removeEventListener(IOErrorEvent.IO_ERROR,urlLoderError);
			urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			this.txtDict[urlLoader.fileName] = urlLoader.data;
			deleteLoader(urlLoader.fileName);
			
			if(this.curIndex == this.urlLoaderAll)
			{
				if(this.rhand != null)
				{
					this.rhand();
				}
			}
		}
		
		private function urlLoderError(e :IOErrorEvent):void
		{
			var urlLoader :CustomURLLoader = e.target as CustomURLLoader;
			urlLoader.removeEventListener(Event.COMPLETE,urlLoaderComplete);
			urlLoader.removeEventListener(IOErrorEvent.IO_ERROR,urlLoderError);
			deleteLoader(urlLoader.fileName);
		}
		/**
		 * 删除loader列表中的加载器 
		 * @param id
		 * 
		 */		
		public function deleteLoader(id :String):void
		{
			if(this.loaderDict[id])
			{
				this.loaderDict[id] = null;
				delete this.loaderDict[id];
			}else
			{
				trace("没有与id相应的Loader" + id);
			}
		}
		
		
		private function completeHandler(event :Event):void
		{
			(event.currentTarget).removeEventListener(Event.COMPLETE,completeHandler);
			(event.currentTarget).removeEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			var loader :CustomLoader = ((event.currentTarget) as LoaderInfo).loader as CustomLoader;
			fileList[loader.url] = loader.url;
			loader.unload();
			deleteLoader(loader.fileName);
			if(this.rhand != null)
			{
				this.rhand();
			}
			this.rhand = null;
		}
		
		private function errorHandler(event :IOErrorEvent):void
		{
			trace("加载出错");
			var loader :CustomLoader = ((event.currentTarget) as LoaderInfo).loader as CustomLoader;
			loader.removeEventListener(Event.COMPLETE,completeHandler);
			loader.removeEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			deleteLoader(loader.fileName);
			
			if(this.fhand != null)
			{
				this.fhand();
			}
			this.fhand = null;
		}

		public static function get instance():LoaderManager
		{
			if(_instance == null)
			{
				_instance = new LoaderManager(new PrivateClass());
			}
			return _instance;
		}

	}
}
class PrivateClass{}